﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pcb.Common
{
    /// <summary>
    /// 日期操作类
    /// </summary>
    public class _Date
    {
        /// <summary>
        /// 判断某日期是否为当前月份的量后一天
        /// </summary>
        /// <param name="Date"> 日期 </param>
        public static bool IsLastDayByMonth(DateTime Date)
        {
            if (Date == DateTime.Today.AddDays(1 - DateTime.Today.Day).AddMonths(1).AddDays(-1))
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// 判断是否为润年
        /// </summary>
        public static bool IsDoubleYear(int year)
        {
            return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ? true : false;
        }

        /// <summary>
        /// 根椐当前时间，获取时间区间为(x天)的结束时间;
        /// </summary>
        /// <param name="x">间隔天数</param>
        public static string[] GetEndDate(int x)
        {
            string[] dates = new string[3];
            DateTime today = DateTime.Now;
            TimeSpan ts = new TimeSpan(x - 1, 0, 0, 0);

            DateTime endDate = today + ts;

            dates[0] = endDate.Year.ToString();
            dates[1] = endDate.Month.ToString();
            dates[2] = endDate.Day.ToString();

            return dates;
        }

        /// <summary>
        /// 根据当前日期，查询前x天的日期数，如：今天为12号且x=10 则数组结果为12,13,14...21
        /// </summary>
        /// <param name="x">间隔天数</param>
        public static string[] GetDays(int x)
        {
            string[] days = new string[x];
            DateTime today = DateTime.Now;

            for (int i = 0; i < x; i++)
            {
                TimeSpan ts = new TimeSpan(i, 0, 0, 0);
                DateTime endDate = today + ts;

                days[i] = endDate.Day.ToString();
            }

            return days;
        }

        /// <summary>
        /// 根据年月日计算星期几
        /// </summary>
        /// <param name="y">年</param>
        /// <param name="m">月</param>
        /// <param name="d">日</param>
        public static string CaculateWeekDay(int y, int m, int d)
        {
            if (m == 1) m = 13;
            if (m == 2) m = 14;
            int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 + 1;
            string weekstr = "";
            switch (week)
            {
                case 1: weekstr = "Mon"; break;
                case 2: weekstr = "Tue"; break;
                case 3: weekstr = "Wed"; break;
                case 4: weekstr = "Thu"; break;
                case 5: weekstr = "Fri"; break;
                case 6: weekstr = "Sat"; break;
                case 7: weekstr = "Sun"; break;
            }

            return weekstr;
        }

        /// <summary>
        /// 根据当前日期，记录各日期属于星期几
        /// </summary>
        /// <param name="x">间隔天数</param>
        public static string[] GetWeek(int x)
        {
            string[] week = new string[x];

            DateTime today = DateTime.Now;

            for (int i = 0; i < x; i++)
            {
                TimeSpan ts = new TimeSpan(i, 0, 0, 0);
                DateTime endDate = today + ts;

                int year = endDate.Year;
                int month = endDate.Month;
                int day = endDate.Day;

                week[i] = CaculateWeekDay(year, month, day);
            }

            return week;
        }

    }
}
